package com.opgea.crm.web.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.opgea.crm.domain.entities.Lead;
import com.opgea.crm.domain.entities.LeadDocuments;
import com.opgea.crm.services.LeadService;

@Controller
@RequestMapping("testdocument")
public class Testdoc {

	@Autowired
	LeadService leadService;

	@RequestMapping(method=RequestMethod.GET,value="processDoc")
	public void process(HttpServletResponse response)
			throws javax.servlet.ServletException, java.io.IOException {
		
		String fileName = "c:/apps/SUNITA.doc";
		File pdffile = new File(fileName);
		
		//lead start here
		
		Lead lead=leadService.findLead(101);
		List<LeadDocuments> docs=lead.getDocuments();
		System.out.println("list of doc"+docs);
		//Lead end here

		ServletOutputStream stream = null;
		BufferedInputStream buf = null;
		try {
			stream = response.getOutputStream();
		
			response.setContentType("application/msword");
			response.setDateHeader("Expires", 0);

			response.addHeader("Content-Disposition",
					"attachment; filename=test.doc");
			response.setContentLength((int) pdffile.length());
			buf = new BufferedInputStream(new FileInputStream(pdffile));

			

			int readBytes = 0;
			
			 while ((readBytes = buf.read()) != -1)
			stream.write(readBytes);
		} finally {
			if (stream != null)
				stream.flush();
			stream.close();
			if (buf != null)
				buf.close();
		}
	}
}
